<?php

namespace app\admin\controller;

use app\models\{Firms, FirmName as Users, User, Suser};
use think\Db;
use app\facade\Redis;
use think\Validate;

class Firm extends Base
{
    public function firms()
    {
        $model = new Firms();
        $data = $this->request->get();
        $results = $model->order('b.id desc')->alias('b')->join('user u', 'u.id= b.user_id')->where('u.isdel', 1);
        if (!empty($data['user_id'])) {
            $results = $results->where('b.user_id', $data['user_id']);
        }
        if (!empty($data['firm_id'])) {
            $results = $results->where('b.firm_id', $data['firm_id']);
        }
        if (!empty($data['paystatus'])) {
            $results = $results->where('b.paystatus', $data['paystatus']);
        }
        if (!empty($data['payment'])) {
            $results = $results->where('b.payment', $data['payment']);
        }
        if (!empty($data['cangku'])) {
            $results = $results->where('u.type', $data['cangku']);
        }
        if (!empty($data['starttime'])) {
            $data['starttime'] = date('Y-m-d', strtotime($data['starttime'])) . ' 00:00:00';
            $endtime = !empty($data['endtime']) ? date('Y-m-d', strtotime($data['endtime'])) . ' 23:59:59' : date('Y-m-d H:i:s');
            $results = $results->whereBetweenTime('b.times', $data['starttime'], $endtime);
        }
        $results = $results->field('b.*,u.type as cangku');
        // a仓库
        if (!empty($this->request->admin->type)) {
            $results = $results->where(['u.type' => $this->request->admin->type]);
            $user = (new Suser())->where(['type' => $this->request->admin->type, 'isdel' => 1])->select() ?? [];
        } else {
            $user = (new Suser())->where(['isdel' => 1])->select() ?? [];
        }
        $bdata['total'] = $results->sum('total');
        $bdata['number'] = $results->sum('number');
        $bdata['atotal'] = $results->sum('atotal');
        $bdata['anumber'] = $results->sum('anumber');
        $bdata['stotal'] = $results->sum('stotal');
        $bdata['snumber'] = $results->sum('snumber');
        $bdata['rtotal'] = $results->sum('rtotal');
        $bdata['rnumber'] = $results->sum('rnumber');
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['paystatus'] = config()["config"]['paystatus'][$result['paystatus']] ?? '';
            $result['payment'] = config()["config"]['payment'][$result['payment']] ?? '';
            $result['cangku'] = config()["config"]['cangku'][$result['cangku']] ?? '';
            $result['user'] = (new Suser())->where('id', $result['user_id'])->find() ?? [];
            $result['firm'] = (new Users())->where('id', $result['firm_id'])->find() ?? [];
            $result['images'] = json_decode($result['images'], true) ?? [];
        }
        $paystatus = config()["config"]['paystatus'] ?? [];
        $payment = config()["config"]['payment'] ?? [];
        $cangku = config()["config"]['cangku'] ?? [];
        $firm = (new Users())->where('isdel', 1)->select() ?? [];
        return view(null, [
            'list' => $results,
            'paystatuss' => $paystatus,
            'payments' => $payment,
            'cangku' => $cangku,
            'user' => $user,
            'firm' => $firm,
            'bdata' => $bdata
        ]);
    }

    public function firms_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '提交频繁,请' . $rdslist . 's后再提交']);
            }
            Redis::setex('clickreturn', 1, 1);
            $data = $this->request->post();
            $validate = new Validate([
                'user_id' => 'require',
                'firm_id' => 'require',
            ], [
                'user_id.require' => '用户名称不能为空',
                'firm_id.require' => '商行不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $data['images'] = $data['images'] ?? [];
            $data['images'] = json_encode($data['images'], true);
            $data['admin_id'] = $this->request->admin->id;
            $model = new Firms();
            if (!empty($data['id'])) {
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $data['admin_id'] = $this->request->admin->id;
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new Firms();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            $paystatus = config()["config"]['paystatus'] ?? [];
            $payment = config()["config"]['payment'] ?? [];
            if (!empty($this->request->admin->type)) {
                $user = (new Suser())->where(['type' => $this->request->admin->type, 'isdel' => 1])->select() ?? [];
            } else {
                $user = (new Suser())->where(['isdel' => 1])->select() ?? [];
            }
            if ($row) {
                $row['images'] = json_decode($row['images'], true);
            }
            $firm = (new Users())->where('isdel', 1)->select() ?? [];
            return view('', [
                'row' => $row,
                'paystatuss' => $paystatus,
                'payments' => $payment,
                'user' => $user,
                'firm' => $firm
            ]);
        }
    }

    public function user()
    {
        $list = (new Users())->order('id desc')->where('isdel', 1);
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('name', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function user_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require'
            ], [
                'name.require' => '名称不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            // if (!empty($this->request->admin->type)) {
            //     $data['type'] = $this->request->admin->type;
            // } else {
            //     if (empty($data['type'])) {
            //         return json(['status' => 1, 'msg' =>  '客户组别不能为空']);
            //     }
            // }
            if (!empty($data['id'])) {
                $res = (new Users())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Users())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Users())->where('id', $id)->find();
            $admin = $this->request->admin;
            $cangku = config()["config"]['cangku'] ?? [];
            return view('', [
                'row' => $row,
                'admin' => $admin,
                'cangku' => $cangku
            ]);
        }
    }

    public function user_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除信息']);
            }
            $banner = (new Users())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '信息不存在，请重试']);
            }
            $res = (new Users())->where('id', $id)->update(['isdel' => 2]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }



    public function suser()
    {
        $list = (new Suser())->order('id desc')->where('isdel', 1);
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('name|mobile', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['cangku'])) {
            $list = $list->where('type', $data['cangku']);
        }
        if (!empty($this->request->admin->type)) {
            $list = $list->where(['type' => $this->request->admin->type]);
        }
        $list = $list->paginate(10);
        $cangku = config()["config"]['cangku'] ?? [];
        foreach ($list as &$lists) {
            $lists['cangku'] = config()["config"]['cangku'][$lists['type']] ?? '';
        }
        return view(null, [
            'list' => $list,
            'cangku' => $cangku
        ]);
    }

    public function suser_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require'
            ], [
                'name.require' => '用户不能为空'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($this->request->admin->type)) {
                $data['type'] = $this->request->admin->type;
            } else {
                if (empty($data['type'])) {
                    return json(['status' => 1, 'msg' =>  '客户组别不能为空']);
                }
            }
            if (!empty($data['id'])) {
                $res = (new Suser())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Suser())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Suser())->where('id', $id)->find();
            $admin = $this->request->admin;
            $cangku = config()["config"]['cangku'] ?? [];
            return view('', [
                'row' => $row,
                'admin' => $admin,
                'cangku' => $cangku
            ]);
        }
    }

    public function suser_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除账号']);
            }
            if (!empty($this->request->admin->type)) {
                $banner = (new Suser())->where('id', $id)->where(['type' => $this->request->admin->type])->find();
            } else {
                $banner = (new Suser())->where('id', $id)->find();
            }
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '用户不存在，请重试']);
            }
            $res = (new Suser())->where('id', $id)->update(['isdel' => 2]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }
}
